package com.kitty.game.pet.handler;

import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.pet.PetDataPool;
import com.kitty.game.pet.bean.PetObject;
import com.kitty.game.pet.message.RespRefineSuccess;
import com.kitty.mina.message.MessagePusher;
import org.nutz.lang.util.NutMap;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Map;
import java.util.Random;

/**
 * 宠物洗炼
 */
@Component("RefineHandler")
public class RefineHandler extends ResetHandler {
    private static final String SUCCESS = "恭喜你，洗练成功，#Y{0}#n的属性已重新生成。";
    private static final String FAIL = "很抱歉，你的#Y{0}#n所有属性已满，无法进行洗练。";

    @Override
    public String acceptFeed(Pet pet, Role role, RoleEquip roleEquip) {
        PetObject petObject = PetDataPool.getPetObject(pet.getName());
        String result = canFeed(pet, roleEquip, 1, petObject);
        if (result != null) {
            return result;
        }
        Random random = new Random();
        NutMap refineMap = NutMap.NEW();
        boolean isTrue = false;
        int max, min;

        Map<String, Integer> growInfo = petObject.getGrowInfo(false, "life");
        max = petService.getMaxValue(growInfo);
        if (pet.getLifeEffect() < max) {
            min = petService.getMinValue(growInfo);
            int life = random.nextInt(max - min) + min + 1;
            if (life > max) {
                life = max;
            }
            refineMap.setv("life", life - pet.getLifeEffect());
            isTrue = true;
        }
        growInfo = petObject.getGrowInfo(false, "mana");
        max = petService.getMaxValue(growInfo);
        if (pet.getManaEffect() < max) {
            min = petService.getMinValue(growInfo);
            int mana = random.nextInt(max - min) + min + 1;
            if (mana > max) {
                mana = max;
            }
            refineMap.setv("mana", mana - pet.getManaEffect());
            isTrue = true;
        }

        growInfo = petObject.getGrowInfo(false, "speed");
        max = petService.getMaxValue(growInfo);
        if (pet.getSpeedEffect() < max) {
            min = petService.getMinValue(growInfo);
            int speed = random.nextInt(max - min) + min + 1;
            if (speed > max) {
                speed = max;
            }
            refineMap.setv("speed", speed - pet.getSpeedEffect());
            isTrue = true;
        }
        growInfo = petObject.getGrowInfo(false, "phy");
        max = petService.getMaxValue(growInfo);
        if (pet.getPhyEffect() < max) {
            min = petService.getMinValue(growInfo);
            int phy = random.nextInt(max - min) + min + 1;
            if (phy > max) {
                phy = max;
            }
            refineMap.setv("phy", phy - pet.getPhyEffect());
            isTrue = true;
        }
        growInfo = petObject.getGrowInfo( false, "mag");
        max =petService.getMaxValue(growInfo);
        if (pet.getMagEffect() < max) {
            min = petService.getMinValue(growInfo);
            int mag = random.nextInt(max - min) + min + 1;
            if (mag > max) {
                mag = max;
            }
            refineMap.setv("mag", mag - pet.getMagEffect());
            isTrue = true;
        }

        if (!isTrue) {
            return MessageFormat.format(FAIL, pet.getName());
        } else {
            equipService.delEquipNum(role, roleEquip, 1);
            if (roleEquip.isEverLimit()) {
                petService.addLimitTime(pet, 1);
            }

            pet.setRefineMap(refineMap);
            MessagePusher.pushMessage(role, new RespRefineSuccess());
            pushOperTypeResult(role,true);
            petService.refreshPetInfo(role, pet);
            return MessageFormat.format(SUCCESS, pet.getName());
        }
    }

    /**
     * 洗炼宝宝 只能是宝宝
     */
    @Override
    protected boolean isRightType(byte petType) {
        return petType != BABY;
    }
}
